package com.example.task1.ui.AllService.EventManagement;

import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.bean.EventBannerBean;
import com.example.task1.bean.EventTabBean;
import com.example.task1.http.Api;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;
import com.google.android.material.tabs.TabLayout;
import com.youth.banner.Banner;

import java.util.List;
import java.util.stream.Collectors;

import retrofit2.Call;

public class EventManagementActivity extends AppCompatActivity {

    private Banner banner;
    private TabLayout tabLayout;
    private ViewPager pager;
    private EventTabAdapter eventTabAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event_management);
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setTitle("活动管理");
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        initView();
        initData();
        setAdapter();
    }

    private void setAdapter() {
        eventTabAdapter = new EventTabAdapter(getSupportFragmentManager());
        pager.setAdapter(eventTabAdapter);
        tabLayout.setupWithViewPager(pager);
    }

    private void initData() {
        Call<ApiResp> bannerCall = Api.api().get(MyApplication.TOKEN, "/prod-api/api/activity/rotation/list");
        Api.send(bannerCall, EventBannerBean.class, new CallBack<EventBannerBean>() {
            @Override
            public void success(EventBannerBean data) {
                List<String> collect = data.getRows().stream().map(EventBannerBean.RowsDTO::getAdvImg).collect(Collectors.toList());
                MyApplication.banner(banner,collect);
            }

            @Override
            public void error(String msg) {

            }
        });
        Call<ApiResp> apiRespCall = Api.api().get(MyApplication.TOKEN, "/prod-api/api/activity/category/list");
        Api.send(apiRespCall, EventTabBean.class, new CallBack<EventTabBean>() {
            @Override
            public void success(EventTabBean data) {
                EventTabBean.DataDTO dataDTO = new EventTabBean.DataDTO();
                dataDTO.setId(0);
                dataDTO.setName("推荐");
                List<EventTabBean.DataDTO> rows = data.getData();
                rows.add(0,dataDTO);
                eventTabAdapter.setData(rows);
            }

            @Override
            public void error(String msg) {

            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    private void initView() {
        banner = (Banner) findViewById(R.id.banner);
        tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        pager = (ViewPager) findViewById(R.id.pager);
        pager.setOffscreenPageLimit(1);
    }
}